package com.docMan.utils;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.docMan.bean.IndexFile;
import com.docMan.lucene.Indexer;
import com.docMan.lucene.Searcher;
import com.docMan.quartz.IndexOaJob;

public class OaFileIndexInit {
	public static void main(String[] args) {
		dbUtils dbu = new dbUtils();
		MydbUtils mydbUtils = new MydbUtils();
		indexOA(dbu);
		// deleteOA(dbu);
		/*
		 * List<Object> list= dbu.excuteQuery("select * from TCHUMAN WHERE 1=1",
		 * null); for (Object object : list) { System.out.println(object); }
		 */
		/*
		 * List<Object> list1=
		 * mydbUtils.excuteQuery("select DA_QYAJB_SEQ.NEXTVAL FROM DUAL", null);
		 * for (Object object : list1) { System.out.println(object.toString());
		 * } ResultSet
		 * rs=dbu.executeQueryRS("select * from TCHUMAN WHERE 1=1",null);
		 * while(rs.next()){ System.out.println(rs.getObject(1)); }
		 */
	}

	public static void indexOA(dbUtils dbu) {
		try {
			Indexer indexer = Indexer.getInstance(FinalConfig.index_dir);
			// List<Object> list=
			// dbu.excuteQuery("select count(*) from TZREC1578", null);
			// BigDecimal count=(BigDecimal) list.get(0);
			int indexCount = 0;
			int i = 0;
			while (indexCount < 10) {
				indexCount = (i + 1) * 10;
				ResultSet rs = dbu
						.executeQueryRS(
								"select VRECID,VSWBH,VLWBH,VLWDW,VLWBT,VLASTDATE,IMPORTANT from DANGAN WHERE  DANGAN.VLASTDATE is not null  ORDER BY VLASTDATE DESC",
								null);
				i++;
				while (rs.next()) {
					IndexFile indexFile = new IndexFile();
					System.out.println(rs.getDate(6).toString());
					indexFile.setFileId(rs.getString(1));
					indexFile.setSWBH(rs.getString(2) == null ? "无" : rs
							.getString(2));
					indexFile.setLWBH(rs.getString(3) == null ? "无" : rs
							.getString(3));
					indexFile.setCreateTime(rs.getDate(6).toString());
					indexFile.setLWDW(rs.getString(5) == null ? "无" : rs
							.getString(4));
					indexFile.setFileName(rs.getString(6) == null ? "无" : rs
							.getString(5));
					indexFile.setLWBT(rs.getString(5) == null ? "无" : rs
							.getString(5));
					indexFile.setDate(rs.getDate(6));
					indexFile.setIndexFrom(FinalConfig.INDEX_FORM_OA);
					indexFile.setContent(indexFile.getFileName() + "-"
							+ indexFile.getSWBH() + "-" + indexFile.getLWDW()
							+ "-" + indexFile.getLWBT() + "-"
							+ indexFile.getLWBH());
					indexFile.setSearchAble(FinalConfig.searchType_all);
					indexFile.setIsImportantFile(null==rs.getString(7)?"否":"是".equals(rs.getString(7)) ? "是": "否");
					indexer.indexOAFile(indexFile);
					System.out.println("make index for the oadocId is "+indexFile.getFileId()+"日期为："+rs.getDate(7).toString());
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void deleteOA(dbUtils dbu) {
		try {
			Indexer indexer = Indexer.getInstance(FinalConfig.index_dir);
			List<Object> list = dbu.excuteQuery("select count(*) from DANGAN",
					null);
			BigDecimal count = (BigDecimal) list.get(0);
			int indexCount = 0;
			int i = 0;
			while (indexCount <= 100) {
				indexCount = (i + 1) * 10;
				ResultSet rs = dbu
						.executeQueryRS(
								"select * from ( select rownum as rn,DANGAN.* from DANGAN) where rn>?and rn<?",
								i * 10, indexCount);
				i++;
				while (rs.next()) {
					indexer.delete("oa" + rs.getObject(2));
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
